﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.Catalog
{
    public partial class frmNhanvien : PRE.Catalog.FrmBase
    {
        DAL.Entities_DAL.Nhanvien_DAL _dal = new DAL.Entities_DAL.Nhanvien_DAL();
        BLL.Entities_BLL.Nhanvien_BLL _bll = new BLL.Entities_BLL.Nhanvien_BLL();
        public frmNhanvien()
        {
            InitializeComponent();
        }

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            DataTable _ma = _dal.Select_max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["id"] + "");
                    txtMaNV.Text = ma;

                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);
                    ResetText();
                    txtHoten.Focus();
                }

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string id = txtMaNV.Text;

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txtHoten.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            txtMaNV.Properties.ReadOnly = true;

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            base.PerformRefresh();
        }
        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            var ten = Proper(txtHoten.Text);
            var diachi = Proper(txtDiachi.Text);
            var chucvu = Proper(txtChucvu.Text);
            var nguyenquan = Proper(txtHKTT.Text);
            if (IsAdd)
            {
                var o = new DAL.Entities.Nhanvien()
                {
                    MA_NV = txtMaNV.Text,
                    HOTEN_NV = ten,
                    TEN_DANG_NHAP = gluTenDN.Text,
                    NGAY_SINH = Convert.ToDateTime(dtNgaysinh.EditValue),
                    CHUC_VU = chucvu,
                    DIA_CHI = diachi,
                    SO_DIEN_THOAI = txtSDT.Text,
                    LUONG = txtMucluong.Text,
                    HKTT = nguyenquan,
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            if (IsEdit)
            {
                var _id = grvMain.GetFocusedRowCellValue("MA_NV") + "";

                var o = new DAL.Entities.Nhanvien()
                {
                    MA_NV = txtMaNV.Text,
                    HOTEN_NV = ten,
                    TEN_DANG_NHAP = gluTenDN.Text,
                    NGAY_SINH = Convert.ToDateTime(dtNgaysinh.EditValue),
                    CHUC_VU = chucvu,
                    DIA_CHI = diachi,
                    SO_DIEN_THOAI = txtSDT.Text,
                    LUONG = txtMucluong.Text,
                    HKTT = nguyenquan,
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            base.PerformSave();
        }
        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txtHoten.Text = null;
            gluTenDN.Text = null;
            txtChucvu.Text = null;
            txtHKTT.Text = null;
            dtNgaysinh.EditValue = DateTime.Now;
            txtDiachi.Text = null;
            txtSDT.Text = null;
            txtMucluong.Text = null;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtMaNV.DataBindings.Clear();
            txtHoten.DataBindings.Clear();
            gluTenDN.DataBindings.Clear();
            txtChucvu.DataBindings.Clear();
            txtHKTT.DataBindings.Clear();
            dtNgaysinh.DataBindings.Clear();
            txtDiachi.DataBindings.Clear();
            txtSDT.DataBindings.Clear();
            txtMucluong.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtMaNV.DataBindings.Add("EditValue", _dtb, ".MA_NV");
            txtHoten.DataBindings.Add("EditValue", _dtb, ".HOTEN_NV");
            gluTenDN.DataBindings.Add("EditValue", _dtb, ".TEN_DANG_NHAP");
            txtChucvu.DataBindings.Add("EditValue", _dtb, ".CHUC_VU");
            dtNgaysinh.DataBindings.Add("EditValue", _dtb, ".NGAY_SINH");
            txtDiachi.DataBindings.Add("EditValue", _dtb, ".DIA_CHI");
            txtHKTT.DataBindings.Add("EditValue", _dtb, ".HKTT");
            txtSDT.DataBindings.Add("EditValue", _dtb, ".SO_DIEN_THOAI");
            txtMucluong.DataBindings.Add("EditValue", _dtb, ".LUONG");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            txtHoten.Properties.ReadOnly = isReadOnly;
            gluTenDN.Properties.ReadOnly = isReadOnly;
            txtChucvu.Properties.ReadOnly = isReadOnly;
            txtHKTT.Properties.ReadOnly = isReadOnly;
            txtDiachi.Properties.ReadOnly = isReadOnly;
            txtSDT.Properties.ReadOnly = isReadOnly;
            txtMucluong.Properties.ReadOnly = isReadOnly;
            dtNgaysinh.Properties.ReadOnly = isReadOnly;

            dsNhanvien.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ClearDataBindings();
            ResetText();
            ChangeStatus(false);
            ReadOnlyControl(false);

            txtMaNV.Properties.ReadOnly = false;
            txtMaNV.ResetText();
            txtMaNV.Focus();

            dsNhanvien.Enabled = true;

            base.PerformSearch();
        }
        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dtb = _dal.Select();

            if (_dtb != null)
            {
                dsNhanvien.DataSource = _dtb;
                string _dem = _dal.Select_count();
                lbNhanvien.Text = _dem.ToString() + " thành viên";
            }

            base.LoadData();
        }
        #endregion

        private void frmNhanvien_Load(object sender, EventArgs e)
        {
            DataTable _dtb3 = _dal.Select_TenDN();
            gluTenDN.Properties.DataSource = _dtb3;
        }
    }
}
